﻿using HtmlAgilityPack;
using MijnMultiOSRSSApp.Common.Models;
using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows.Data;

namespace MijnMultiOSRSSApp.Common.Converters
{
    public class SyndicationToImageConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null || !(value is RSSItem))
                return null;

            var item = value as RSSItem;
            var links = item.Link;
            if (links == null)
                return null;

            var document = new HtmlDocument();
            if (item.Description != null)
                document.LoadHtml(item.Description);
            var images = document.DocumentNode.Descendants("img").ToList();
            return images.Select(x => x.GetAttributeValue("src", "")).FirstOrDefault(x => !string.IsNullOrEmpty(x));
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
